//
//  UIAlertController+HandleError.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/9.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "UIAlertController+HandleError.h"

///通过 Categoty 给 UIAlertController 增加一个实例化的类方法
@implementation UIAlertController (HandleError)

+ (instancetype)showError:(NSError *)error ActionHandler:(void (^)(NSString *buttonTitle))handler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:error.localizedDescription message:error.localizedRecoverySuggestion preferredStyle:UIAlertControllerStyleAlert];
    [error.localizedRecoveryOptions enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         UIAlertAction *action = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             handler(obj);
         }];
         [alertController addAction:action];
     }];
    return alertController;
}

@end
